package com.hj.ct.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.hj.ct.App;
import com.hj.ct.R;
import com.hj.ct.db.dao.UserDao;
import com.hj.ct.logic.LogicActions;
import com.hj.ct.logic.LoginLogic;
import com.hj.ct.model.User;
import com.hj.ct.util.AndroidUtil;
import com.hj.ct.util.SharePreferenceUtil;
import com.hj.ct.view.CustomDialog;

public class LoginActivity extends BaseActivity implements OnClickListener {

	private EditText etName, etPassword;
	private Button btnLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initView();
		initData();
	}

	private void initView() {
		etName = (EditText) findViewById(R.id.et_userName);
		etPassword = (EditText) findViewById(R.id.et_password);
		btnLogin = (Button) findViewById(R.id.btn_login);
		btnLogin.setOnClickListener(this);
		findViewById(R.id.btn_cancel).setOnClickListener(this);
	}

	private void initData() {
		String userName = SharePreferenceUtil.getInstance(App.getContext()).getString("currentUserName", "");
		etName.setText(userName);
		User user = new UserDao(App.getContext()).getUser(userName);
		if (user != null) {
			etPassword.setText(user.getPassword());
			btnLogin.performClick();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.btn_login:
				verify();
				break;
			case R.id.btn_cancel:
				finish();
				AndroidUtil.exits();
				break;
			default:
				break;
		}
	}

	private void verify() {
		hideSoftInput(this, getWindow().getDecorView());
		String account = etName.getText().toString();
		String password = etPassword.getText().toString();
		if (TextUtils.isEmpty(account)) {
			toast("用户名不能为空");
			return;
		}
		if (TextUtils.isEmpty(password)) {
			toast("密码不能为空");
			return;
		}
		LoginLogic.getInstance().login(this, account, password);
		showProgressDialog("正在登陆，请稍候");
	}

	public void onBackPressed() {
		final CustomDialog dialog = new CustomDialog(this);
		dialog.setTitile("提示");
		dialog.setMessage("\n 确定要退出吗？                                  \n");
		dialog.setLeftBtn("取消");
		dialog.setRightBtn("确定");
		dialog.setLeftBtnOnclickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setRightBtnOnclickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
				AndroidUtil.exits();
			}
		});
		dialog.show();
	}

	@Override
	public void handlerUI(int aciton, int code, Object... obj) {
		// TODO Auto-generated method stub
		dismissDialog();
		if (aciton == LogicActions.LOGIN_SUCCESS) {
			startActivity(new Intent(this, MainActivity2.class));
			finish();
		} else if (aciton == LogicActions.LOGIN_ERROR) {
			toast((String) obj[0]);
		}
	}
}
